package org.zhouhe.tag.article;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.zhouhe.modules.article.service.ArticleService;
import org.zhouhe.util.Utils;
import org.zhouhe.vo.Article;
import org.zhouhe.vo.ArticleType;

public class ArticleWithRecoByIndustry1  extends TagSupport {

	private static final long serialVersionUID = -3669862006870055335L;
	private ServletContext servletContext;
	private WebApplicationContext wac;
	private ArticleService articleService;

	private Integer typeId;
	private Integer industryId;
	private Integer resultSize = 9;
	private Integer lineLen = 10;
	private Integer summeryLen = 100;
	private String className;
	
	public int doStartTag() {
		try {
			StringBuilder html = new StringBuilder();
			HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
			servletContext = request.getSession().getServletContext();
			wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
			articleService = (ArticleService) wac.getBean("articleService");
			ArticleType articleType = articleService.getArticleType(typeId);
			html.append("<div class='"+ className +"'>");
			html.append("<ul class='blockTag-title-ul'>");
			html.append("  <li class='blockTag-title-text'>"+ articleType.getName() +"</li>");
			html.append("  <li style='float:right'><a target='_blank' href='articleBlocks4t1ix.action?articleType.articleTypeId="+ typeId +"'>更多&gt;&gt;</a></li>");
			html.append("</ul>");
			List<Article> articleList = articleService.getRecommendByIndustry1(typeId, industryId, resultSize);
			Article recoArticle = null;
			if(articleList.size()>0){
				recoArticle = articleList.get(0);
				articleList.remove(0);
			}else{
				recoArticle = new Article();
			}
			html.append("  <div>");	
			html.append("    <div class='block-article-reco-div'>");
			html.append("      <div class='block-article-reco-title-div'>");
			html.append("        <a href='article.action?article.articleId="+ recoArticle.getArticleId() +"'>"+ recoArticle.getTitle() +"</a>");
			html.append("      </div>");
			html.append("      <div class='block-article-reco-content-div'>");
			html.append("        "+ Utils.formatTitle(recoArticle.getSummary(), summeryLen));
			html.append("        <a href='article.action?article.articleId="+ recoArticle.getArticleId() +"'>阅读全文&gt;&gt;</a>");
			html.append("      </div>");
			html.append("    </div>");
			html.append("    <div class='block-article-reco-list-div'>");
			html.append("      <ul>");
			for(Article atc : articleList){
				html.append("        <li>• <a href='article.action?article.articleId="+ atc.getArticleId() +"' title='"+ atc.getTitle() +"'>"+ Utils.formatTitle(atc.getTitle(), lineLen) +"</a></li>");//
			}
			html.append("      </ul>");
			html.append("    </div>");
			html.append("  </div>");
			html.append("  <div style='clear:both;'></div>");
			html.append("</div>");
			JspWriter out = pageContext.getOut();
			out.print(html.toString());
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SKIP_BODY;
	}
	
	
	public Integer getTypeId() {
		return typeId;
	}
	public void setTypeId(Integer typeId) {
		this.typeId = typeId;
	}
	public Integer getResultSize() {
		return resultSize;
	}
	public void setResultSize(Integer resultSize) {
		this.resultSize = resultSize;
	}
	public String getClassName() {
		return className;
	}
	public void setClassName(String className) {
		this.className = className;
	}

	public void setLineLen(Integer lineLen) {
		this.lineLen = lineLen;
	}


	public Integer getLineLen() {
		return lineLen;
	}


	public void setSummeryLen(Integer summeryLen) {
		this.summeryLen = summeryLen;
	}


	public Integer getSummeryLen() {
		return summeryLen;
	}


	public void setIndustryId(Integer industryId) {
		this.industryId = industryId;
	}


	public Integer getIndustryId() {
		return industryId;
	}
}
